package com.njupt.Tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/11/19 9:55
 * @Description: 二叉树前序遍历
 * @Version: 1.0
 */


public class PreorderTraversal {


    public void preOrder(TreeNode root,List<Integer> result){
        if(root == null){
            return;
        }
        result.add(root.val);
        preOrder(root.left,result);
        preOrder(root.right,result);
    }

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        preOrder(root,result);
        return result;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode node1 = new TreeNode(2);
        TreeNode node2 = new TreeNode(3);
        root.right = node1;
        node1.left = node2;
        PreorderTraversal test = new PreorderTraversal();
        List<Integer> list = test.preorderTraversal(root);
        System.out.println(list.toString());
    }
}
